package happychat;

import javax.swing.*;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends JFrame {

	private static int ppp = 12345;
	static InetAddress addr;
	static String ip;
	static String address;
	public Server() {
		this.setTitle("服务器");

	}

	public static void main(String arg[]) throws IOException {

		addr = InetAddress.getLocalHost();
		ip = addr.getHostAddress();// 获得本机IP
		address = addr.getHostName();// 获得本机名称

		new Server();
		// 负责收,存
		// 建立多线程服务器
		// 接收文件名
		// 判断本地是否存在那个文件,结果发送到客户端
		// 用InputStream,FileOutStream读取数据,输出到到本地

		ServerSocket server = new ServerSocket(ppp);// 指定端口号
		System.out.println("服务器启动" + "\r\n" + "内网IP地址:" + ip + "\r\n" + "绑定端口号:" + ppp + "\r\n" + "状态:成功" + "\r\n" + "主机名:"
				+ address);
			while (true) {// 建立多线程服务器
			Socket socket = server.accept();// 接收请求
			new Thread() {
				public void run() {
					try {
						System.out.println("已分配线程");
						BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));// 输入流
						PrintStream ps = new PrintStream(socket.getOutputStream());// 输出流
						InputStream is = socket.getInputStream();// emmmm BufferedReader没法读字节

						String getIP = br.readLine();// 服务端接收文件名
						String getMessage = br.readLine();// 服务端接收文件名

						ps.println("这里是要发送给的人的ip");
						ps.println("这里是输入框的信息");// 将消息发送给服务端


					} catch (IOException e) {
						e.printStackTrace();
					}

				}
			}.start();
		}
	}

}
